package com.mywork.controller;

import java.io.PrintWriter;
import java.security.acl.Permission;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.EJB;
import javax.print.attribute.HashAttributeSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.alibaba.fastjson.JSON;
import com.mywork.dao.EquipmentConsumptionMapper;
import com.mywork.dao.UsedrugMapper;
import com.mywork.po.ConsumptionView;
import com.mywork.po.Cureitem_list;
import com.mywork.po.CureprocessView;
import com.mywork.po.Drug;
import com.mywork.po.Page;
import com.mywork.po.Patient;
import com.mywork.po.PayDetailsList;
import com.mywork.po.PayList;
import com.mywork.po.PayListView;
import com.mywork.po.RecipeDiagnosisView;
import com.mywork.po.RecipeSignView;
import com.mywork.po.RecipeView;
import com.mywork.po.Refund;
import com.mywork.po.RefundView;
import com.mywork.po.Register;
import com.mywork.po.RegisterView;
import com.mywork.po.SaveDrugView;
import com.mywork.po.UseDrugView;
import com.mywork.po.User;
import com.mywork.service.CureitemService;
import com.mywork.service.CureprocessService;
import com.mywork.service.DrugService;
import com.mywork.service.EquipmentConsumptionService;
import com.mywork.service.MyJSONUtil;
import com.mywork.service.PageUtie;
import com.mywork.service.PatientService;
import com.mywork.service.PayService;
import com.mywork.service.PermissionUtil;
import com.mywork.service.RecipeDiagnosisService;
import com.mywork.service.RecipeService;
import com.mywork.service.RecipeSignnService;
import com.mywork.service.RefundService;
import com.mywork.service.RegisterService;
import com.mywork.service.ShowInformation;
import com.mywork.service.UseDrugService;
import com.mywork.service.UserService;
import com.sun.tracing.dtrace.ModuleAttributes;

@Controller
@SessionAttributes({"user","permission"})
@RequestMapping("/drugManager")
public class DrugManagerController {
	@Autowired
	PayService payService;
	@Autowired
	CureitemService cureitemService;
	@Autowired
	RecipeService recipeService;
	
	@Autowired
	RegisterService registerService;
	@Autowired
	DrugService drugService;
	
	@Autowired
	UserService userService;
	
	@Autowired
	UseDrugService useDrugService;
	
	@Autowired
	EquipmentConsumptionService ecs;
	
	@Autowired
	PatientService patientService;
	
	@Autowired
	RecipeDiagnosisService recipeDiagnosisSevice;
	
	@Autowired
	RecipeSignnService recipeSignService;
	
	@Autowired
	CureprocessService cureProcessService;
	
	@Autowired
	RefundService refundService;
	
	@ModelAttribute("drug")
	public Drug initDrug(Drug drug,Model model){
		if(drug==null){
			return new Drug();
		}else{
			return drug;
		}
	}
	//查看具体退费处方具体信息
	@RequestMapping("/getRefundDetails")
	public String getRefundDetails(String recipeId,Model model,HttpServletRequest request,@ModelAttribute("permission")int permission,@ModelAttribute("user")User user){
		
		if(!PermissionUtil.isHavePermission(user, permission, 3)){
			return "/login";
		}
		RecipeView recipeView=recipeService.getRecipeByRecipeId(recipeId);//获取处方视图（医生名，病人名）
		List<UseDrugView> useDrugView =  useDrugService.getUseDrugByRecipeId(recipeId);//获取用物表列表
		List<Cureitem_list> cureItemView = cureitemService.getCureItemByRecipeId(recipeId);//获取处方治疗项目列表
		List<RecipeSignView> signView = recipeSignService.getRecipeSignByRecipeId(recipeId);//获取处方体格诊断列表
		List<RecipeDiagnosisView> diagnosisView = recipeDiagnosisSevice.getRecipeDiagnosisByRecipeId(recipeId);//获取处方治疗表
		List<CureprocessView> cureprocessView = cureProcessService.getCureProcessByRecipeId(Long.valueOf(recipeId).longValue());//获取治疗记录列表
		model.addAttribute("recipeView", recipeView);
		model.addAttribute("useDrugView", useDrugView);
		model.addAttribute("cureItemView", cureItemView);
		model.addAttribute("cureprocessView", cureprocessView);
		model.addAttribute("signView", signView);
		model.addAttribute("diagnosisView",diagnosisView);
		
		
		
		return "/prescription/prescriptionMessage";
	}
	//查看退费统计列表
	@RequestMapping("/getRefundList")
	public String getRefundList(String pageNo,@ModelAttribute("user")User user,@ModelAttribute("permission")int permission,HttpServletRequest request,Model model){
		if(!PermissionUtil.isHavePermission(user, permission, 3)){
			return "/login";
		}
		Page page = PageUtie.getPage(pageNo, 10, refundService.getCountOfAllRefund());
		List<RefundView> refunds =  refundService.getAllRefundList(page);
		model.addAttribute("allPageNo", page.getTopPageNo());
		model.addAttribute("refunds", refunds);
		return "/pay/refundStatistics";
	}
	//查看处方缴费细节列表
	@RequestMapping("/getPaymentDetails")
	public String getPaymentDetails(String recipeId,@ModelAttribute("user")User user,@ModelAttribute("permission")int permission,Model model,HttpServletRequest request){
		if(!PermissionUtil.isHavePermission(user, permission, 3)){
			return "/login";
		}
		RecipeView recipe=recipeService.getRecipeByRecipeId(recipeId);//获取处方
		
		List<RecipeDiagnosisView> recipeDiagnosisView = recipeDiagnosisSevice.getRecipeDiagnosisByRecipeId(recipeId);//获取诊断
		List<RecipeSignView> recipeSignView = recipeSignService.getRecipeSignByRecipeId(recipeId);//获取体格诊断
	
		List<UseDrugView> useDrugView=useDrugService.getUseDrugByRecipeId(recipeId);
		List<Cureitem_list> cureitem = cureitemService.getCureItemByRecipeId(recipeId);
		PayListView payListView = payService.getPayListByRecipeId(recipeId);
		model.addAttribute("recipe", recipe);
		
		model.addAttribute("useDrugView", useDrugView);
		model.addAttribute("cureitem", cureitem);
		model.addAttribute("refund",new Refund());
		model.addAttribute("recipeDiagnosisView", recipeDiagnosisView);
		model.addAttribute("recipeSignView", recipeSignView);
		model.addAttribute("payList", payListView);
		return "/pay/paymentDetails";
		
	}
	//获取缴费列表
	@RequestMapping("/getPayList")
	public String getPayList(String pageNo,@ModelAttribute("user")User user,@ModelAttribute("permission")int permission,Model model,HttpServletRequest request){
		if(!PermissionUtil.isHavePermission(user, permission, 3)){
			return "/login";
		}
		Page page= PageUtie.getPage(pageNo,10,payService.getCountOfAllPayList());
		List<PayListView> payListViews = payService.getAllPayList(page);
		model.addAttribute("allPageNo", page.getTotalRecords());
		model.addAttribute("payListViews",payListViews);
		return "/pay/paymentStatistics";
	}
	//获取挂号列表
	@RequestMapping("/getRegisteredList")
	public String getRegisteredList(String pageNo,@ModelAttribute("user")User user,@ModelAttribute("permission")int permission,Model model,HttpServletRequest request){
		if(!PermissionUtil.isHavePermission(user, permission, 3)){
			return "/login";
		}
		Page page = new Page();
		if(pageNo==null||pageNo==""){
			page.setPageNo(0);
		}else{
			page.setPageNo(Integer.parseInt(pageNo));
		}
		page.setPageSize(10);
		List<RegisterView> registerViews=registerService.getAllregister(page);
		int  allPageNo = registerService.getCountOfAllRegister();
		model.addAttribute("registerViews", registerViews);
		model.addAttribute("allPageNo", allPageNo);
		return "/pay/registeredStatistics";
	}
	//处方退费
	@RequestMapping("/refundForRecipe")
	public String refundRegister(String cardId,@ModelAttribute("user")User user,@ModelAttribute("permission")int permission,HttpServletRequest request,Model model){
		if(!PermissionUtil.isHavePermission(user, permission, 3)){
			return "/login";
		}
		if(cardId==null||cardId==""){
			return "/pay/refundList";
		}else{
			List<RecipeView> recipes=recipeService.getRecipeBycardId(cardId);
			if(recipes.size()==0){
				ShowInformation.forInformation(model, "对不起查不到该用户信息","返回",request.getContextPath()+"/drugManager/payForRecipe", 1);
				return "/information";
			}
			model.addAttribute("recipes",recipes);
			return "/pay/refundList2";
		}
	}
	
	//为退费单添加信息

	@RequestMapping("/showRefundForRecipe")
	public String showRefundForRecipe(String recipeId,@ModelAttribute("user")User user,@ModelAttribute("permission")int permission,HttpServletRequest request,Model model){
		if(!PermissionUtil.isHavePermission(user, permission, 3)){
			return "/login";
		}
	
		RecipeView recipe=recipeService.getRecipeByRecipeId(recipeId);
		List<UseDrugView> useDrugView=useDrugService.getUseDrugByRecipeId(recipeId);
		List<Cureitem_list> cureitem = cureitemService.getCureItemByRecipeId(recipeId);
		List<RecipeDiagnosisView> recipeDiagnosisView = recipeDiagnosisSevice.getRecipeDiagnosisByRecipeId(recipeId);
		List<RecipeSignView> recipeSignView = recipeSignService.getRecipeSignByRecipeId(recipeId);
		List<CureprocessView> cureProcessView = cureProcessService.getCureProcessByRecipeId(Long.parseLong(recipeId));
		model.addAttribute("recipe", recipe);
		model.addAttribute("useDrugView", useDrugView);
		model.addAttribute("cureitems",cureitem);
		model.addAttribute("refund",new Refund());
		model.addAttribute("recipeDiagnosisView", recipeDiagnosisView);
		model.addAttribute("recipeSignView", recipeSignView);
		model.addAttribute("cureProcessView", cureProcessView);
		return "/pay/refundList3";
		
	}
	//修改退费单信息
	@RequestMapping("/changeIsRun")
	public String changeisRun(Refund refund,@ModelAttribute("user")User user,@ModelAttribute("permission")int permission,HttpServletRequest request,Model model){
		if(!PermissionUtil.isHavePermission(user, permission, 3)){
			return "/login";
		}
		refundService.refund(refund,user.getUserid());
			ShowInformation.forInformation(model, "添加成功", "返回", request.getContextPath()+"/drugManager/payForRecipe", 4);
			return "/information";
		
	}
	//处方缴费
	@RequestMapping("/payForRecipe")
	public String payRegister(String cardId,@ModelAttribute("user")User user,@ModelAttribute("permission")int permission,HttpServletRequest request,Model model){
		if(!PermissionUtil.isHavePermission(user, permission, 3)){
			return "/login";
		}
		if(cardId==null||cardId==""){
			return "/pay/payList";
		}else{
			List<RecipeView> recipes=recipeService.getRecipeBycardId(cardId);
			if(recipes.size()==0){
				ShowInformation.forInformation(model, "对不起查不到该用户信息","返回",request.getContextPath()+"/drugManager/payForRecipe", 1);
				return "/information";
			}
			model.addAttribute("recipes",recipes);
			return "/pay/payList2";
		}
		
	}
	//修改缴费单信息
	@RequestMapping("/changeCharge")
	public String changeCharge(String[] cureId,String[] curePrice,String[] cureDiscountPrice,String[] cureSpecialDiscountPrice,String[] drugId,String[] drugPrice,String[] drugDiscountPrice,String[] drugSpecialDiscountPrice,PayList payList,String recipeId,@ModelAttribute("user")User user,@ModelAttribute("permission")int permission,HttpServletRequest request,Model model){
		if(!PermissionUtil.isHavePermission(user, permission, 3)){
			return "/login";
		}
			payService.pay(payList,cureId,curePrice,cureDiscountPrice,cureSpecialDiscountPrice,drugId,drugPrice,drugDiscountPrice,drugSpecialDiscountPrice,user.getUserid());
			ShowInformation.forInformation(model, "添加成功", "返回", request.getContextPath()+"/drugManager/payForRecipe", 4);
			return "/information";
		
	}
	//为处方缴费单添加信息
	@RequestMapping("/showPayForRecipe")
	public String showPayForRecipe(String recipeId,@ModelAttribute("user")User user,@ModelAttribute("permission")int permission,HttpServletRequest request,Model model){
		if(!PermissionUtil.isHavePermission(user, permission, 3)){
			return "/login";
		}
	
		RecipeView recipe=recipeService.getRecipeByRecipeId(recipeId);
		List<UseDrugView> useDrugView=useDrugService.getUseDrugByRecipeId(recipeId);
		List<Cureitem_list> cureitem = cureitemService.getCureItemByRecipeId(recipeId);
		model.addAttribute("recipe", recipe);
		model.addAttribute("useDrugView", useDrugView);
		model.addAttribute("cureitems",cureitem);
		model.addAttribute("payList",new PayList());
		return "/pay/payList3";
		
	}
	
	//进行挂号
	@RequestMapping("/addRegister")
	public String addRegister(@ModelAttribute("user")User user,@ModelAttribute("permission")int permission,Model model,HttpServletRequest request,Register register){
		if(!PermissionUtil.isHavePermission(user, permission, 3)){
			return "/login";
		}
		registerService.insert(register);
		ShowInformation.forInformation(model, "挂号成功", "返回", request.getContextPath()+"/drugManager/register", 4);
		return "information";
	}
	
	//通过卡号找到病人,并将进行挂号的控制器
	@RequestMapping("/register")
	public String register(String userId,String type,String cardId,@ModelAttribute("user")User user,@ModelAttribute("permission")int permission,Model model,HttpServletRequest request){
		if(!PermissionUtil.isHavePermission(user, permission, 3)){
			return "/login";
		}
	
		if(cardId==null||cardId==""){
			List<User> doctors = userService.getAllDoctor();
			model.addAttribute("doctors", doctors);
			return "/pay/registeredList";
		}else{
			Patient patient = patientService.getPatientByCardIdOrName(cardId);
			if(patient==null||patient.getPatientid()==null){
				ShowInformation.forInformation(model, "对不此，查无此病人信息", "返回", request.getContextPath()+"/drugManager/register", 1);
				return "information";
			}else{
				Register register = new Register();
				register.setDate(new Date());
				register.setPatientid(patient.getPatientid());
				register.setPrice(userService.selectById(userId).getRecipepay().longValue());
				register.setType(type);
				register.setUserid(userId);
				
				model.addAttribute("doctor", userService.selectById(userId));
				model.addAttribute("patient",patient);
				model.addAttribute("register",register);
				return "/pay/registeredList2";
			}
		}
	
		
	}
	
	//药品查看进货表
	@RequestMapping("/getPurchaseList")
	public String getPurchaseList(String var,String pageNo,@ModelAttribute("user")User user,@ModelAttribute("permission")int permission,Model model,HttpServletRequest request){
		if(!PermissionUtil.isHavePermission(user, permission, 3)){
			return "login";
		}
		//是否通过var做搜寻工作
		if(var==null||var==""){
			Page page = PageUtie.getPage(pageNo, 10, drugService.getCountOfSaveDrugList());
			List<SaveDrugView> drugs = drugService.getSaveDrugList(page);
			model.addAttribute("allPageNo", page.getTotalPage());
			model.addAttribute("drugs", drugs);
			return "/medicine/purchaseMedicineList";
		}else{
			Page page = PageUtie.getPage(pageNo, 10, drugService.getCountOfSaveDrugList());
			List<SaveDrugView> drugs = drugService.getSaveDrugListByDrugIdOrCode(page,var);
			model.addAttribute("allPageNo", page.getTotalPage());
			model.addAttribute("drugs", drugs);
			return "/medicine/purchaseMedicineList";
		}
		
		
	}
	//药品入库
	@RequestMapping("/saveMedicine")
	public String addMedicine(String addCount,Drug drug,String drugId,@ModelAttribute("user")User user,@ModelAttribute("permission")int permission,Model model,HttpServletRequest request){
		if(!PermissionUtil.isHavePermission(user, permission, 3)){
			return "/login";
		}
		if(drug.getDrugid()==null){
			Drug rowDrug = drugService.getDrugByDrugId(drugId);
			model.addAttribute("drug", rowDrug);
			return "/medicine/addMedicineCount";
		}else{
			System.out.print(drug.getDrugname());
			drugService.addDrugForHadHad(drug, user.getUserid(),addCount);
			ShowInformation.forInformation(model, "入库成功", "返回", request.getContextPath()+"/drugManager/getDrugList", 4);
			return "information";
		}
		
		
	}
	
	//查看分发列表
	@RequestMapping("/getDeliverList")
	public String getDeliverList(String pageNo,@ModelAttribute("user")User user,@ModelAttribute("permission")int permission,Model model,HttpServletRequest request){
		if(!PermissionUtil.isHavePermission(user, permission, 3)){
			return "login";
		}
		Page page = PageUtie.getPage(pageNo, 10, ecs.getCountOfList());
		List<ConsumptionView> useDrugs= ecs.getAll(page);
		model.addAttribute("useDrugs", useDrugs);
		return "/medicine/deliverList";
	}
	
	
	//添加分发药品
	
	@RequestMapping("/deliverMedicine")
	public String addDeliverMdeicine(String[] drugId,String [] count,String userId,@ModelAttribute("user")User user,@ModelAttribute("permission")int permission,Model model,HttpServletRequest request){
		if(!PermissionUtil.isHavePermission(user, permission, 3)){
			return "login";
		}
		if(drugId!=null&&drugId.length!=0&&count!=null&&count.length!=0&&userId!=null&&userId.trim()!=""){
			ecs.addConsumption(userId,drugId,count);
			ShowInformation.forInformation(model, "分发登记成功", "返回",request.getContextPath()+"/drugManager/deliverMedicine", 4);
			return "information";
			
		}else{
			List<User> users = userService.getUsers();
			model.addAttribute("users", users);
			return "/medicine/deliverMedicine";
		}
		
	}
	
	
	//药物使用表
	@RequestMapping("/useDrugList")
	public String getUseDrugList(String pageNo,String drugId,@ModelAttribute("user")User user,@ModelAttribute("permission")int permission,Model model,HttpServletRequest request){
		if(!PermissionUtil.isHavePermission(user, permission, 3)){
			return "/login";
		}
		Page page = PageUtie.getPage(pageNo, 10, useDrugService.getCountOfUseDrugByDrugId(drugId));
		List<UseDrugView> useDrugs = useDrugService.getUseDrugByDrugId(page,drugId);
		if(useDrugs.size()==0){
			ShowInformation.forInformation(model, "该药物没被使用","返回", request.getContextPath()+"/drugManager/getDrugList", 1);
			return "information";
		}
		model.addAttribute("allPageNo", page.getTopPageNo());
		model.addAttribute("drugName", useDrugs.get(0).getDrugname());
		model.addAttribute("useDrugs", useDrugs);
		return "/medicine/medicineMessage";
	}
	
	//药品报损表
	@RequestMapping("/expiredMedicine")
	public String expiredMedicine(String pageNo,@ModelAttribute("user")User user,@ModelAttribute("permission")int permission,Model model,HttpServletRequest request){
		if(!PermissionUtil.isHavePermission(user, permission, 3)){
			return "/login";
		}
		Page page = new Page();
		if(pageNo==null||pageNo==""){
			page.setPageNo(0);
		}else{
			page.setPageNo(Integer.valueOf(pageNo).intValue());
		}
		page.setPageSize(10);
		page.setTotalRecords(drugService.getCountOfExpiredDrugList());
		List<Drug> drugs = drugService.getExpiredDrugList(page);
		model.addAttribute("allPageNo", page.getTotalPage());
		model.addAttribute("drugs", drugs);
		return "/medicine/expiredMedicine";
	}
	
	
	//药品警报列表
	@RequestMapping("/inventoryMedicineNotice")
	public String inventoryMedicineNotice(String pageNo,Model model,@ModelAttribute("user")User user,@ModelAttribute("permission")int permission,HttpServletRequest request){
		if(!PermissionUtil.isHavePermission(user, permission, 3)){
			return "login";
		}
		Page page = new Page();
		if(pageNo==null||pageNo==""){
			page.setPageNo(0);
		}else{
			page.setPageNo(Integer.valueOf(pageNo).intValue());
		}
		page.setPageSize(10);
		page.setTotalRecords(drugService.getCountOfNoticeDrugList());
		List<Drug> drugs = drugService.getDrugWhichNotice(page);
		
		model.addAttribute("allPageNo", page.getTotalPage());
		model.addAttribute("drugs", drugs);
		return "/medicine/inventoryMedicineNotice";
		
	}
	
	//拆零药品
	@RequestMapping("/dismountedMedicine")
	public String dismountedMedicine(Drug drug,String dismountNum,String drugId,@ModelAttribute("user")User user,@ModelAttribute("permission")int permission,Model model,HttpServletRequest request){
		if(user==null||permission!=3){
			return "login";
		}
		if(drug.getDrugid()==null){
			Drug rowDrug=drugService.getDrugByDrugId(drugId);
			model.addAttribute("drug", rowDrug);
			return "/medicine/dismountedMedicine";
		}else{
			Long rowDrugId = drug.getDrugid();
			drug.setDrugid(null);
			drugService.dismount(rowDrugId,dismountNum,drug);
			ShowInformation.forInformation(model, "拆零成功", "返回", request.getContextPath()+"/drugManager/getDrugList", 4);
			return "/information";
		}
		
		
	}
	
	//通过药品名或助记码查找药品
	@RequestMapping("/searchByDrugNameOrMnemonicCode")
	public String getSearchByDrugNameOrMnemonicCode(String drugNameOrMnemonicCode,@ModelAttribute("user")User user,@ModelAttribute("permission")int permission,Model model,HttpServletRequest request){
		if(user==null||permission!=3){
			return "login";
		}
		List<Drug> drugs = drugService.getDrugByDrugNameOrMnemonicCode(drugNameOrMnemonicCode);
		model.addAttribute("drugs", drugs);
		return "/medicine/medicineList";
	}
	
	//通过名字或卡号查找病人
	@RequestMapping("searchByNameOrCardId")
	public String searchByNameOrCardId(String nameOrCardId,@ModelAttribute("user")User user,@ModelAttribute("permission")int permission,Model model,HttpServletRequest request){
		if(user==null||permission!=3){
			return "login";
		}
		if(nameOrCardId==null||nameOrCardId.trim()==""){
			return "/card/cardSearch";
		}
		Patient patient =null;
		try {
			patient = patientService.getPatientByCardIdOrName(nameOrCardId);
		} catch (Exception e) {
			// TODO: handle exception
			ShowInformation.forInformation(model, "对不起,记录可能重名,请用卡号进行查询", "返回", request.getContextPath()+"/drugManager/searchByNameOrCardId", 1);
			return "information";
		}
		
		if(patient==null||patient.getPatientid()==null){
			ShowInformation.forInformation(model, "对不起查无此记录", "返回", request.getContextPath()+"/drugManager/searchByNameOrCardId", 1);
			return "information";
		}else{
			model.addAttribute("patient", patient);
			return "/card/modifyCardMessage";
		}
	}
	
	//获取药物列表
	@RequestMapping("/getDrugList")
	public String getDrugList(String pageNo,Model model,@ModelAttribute("user")User user,@ModelAttribute("permission")int permission){
		if(user==null||permission!=3){
			return "login";
		}
		Page page  = new Page();
		if(pageNo==""||pageNo==null){
			page.setPageNo(0);
		}else{
			page.setPageNo(Integer.valueOf(pageNo).intValue());
		}
		page.setPageSize(10);
		page.setTotalRecords(drugService.getCountOfDrugList());
		List<Drug> drugs = drugService.getDrugList(page);
		model.addAttribute("drugs", drugs);
		model.addAttribute("allPageNum",page.getTotalPage());
		return "/medicine/medicineList";
	}
	
	
	//第一次添加药品
	@RequestMapping("/addDrug")
	public String addDrug(Drug drug,Model model,@ModelAttribute("permission")int permission,HttpServletRequest request){
		User rowUser = (User)request.getSession().getAttribute("user");
		if(rowUser==null||permission!=3){
			return "login";
		}
		if(drug.getDrugname()!=null){
			float price = drug.getPrice();
			if(price==0.00){
				drug.setDrugcan(false);
			}else{
				drug.setDrugcan(true);
			}
			drugService.addDrug(drug,rowUser.getUserid());
			ShowInformation.forInformation(model, "添加成功", "返回", request.getContextPath()+"/drugManager/addDrug", 4);
			return "information";
		}else{
			List<Drug> drugsForLevelOne =  drugService.getAllFirstLevelDrug();
			model.addAttribute("drugsForLevelOne",drugsForLevelOne);
			return "/medicine/addMedicine";
		}
		
		
	}
	
	//删除药品
	@RequestMapping("/deleteDrug")
	public String delectDrug(String drugId,Model model,@ModelAttribute("user")User rowUser,@ModelAttribute("permission")int permission,HttpServletRequest request){
		if(rowUser==null||permission!=3){
			return "/login";
		}
		drugService.delectDrug(drugId);
		ShowInformation.forInformation(model, "删除成功", "返回", request.getContextPath()+"/drugManager/getDrugList", 4);
		return "information";
	}
	
	//更新药品
	@RequestMapping("/updateDrug")
	public String updateDrug(Drug drug,Model model,String drugId,@ModelAttribute("user")User user,@ModelAttribute("permission")int permission,HttpServletRequest request){
		if(user==null||permission!=3){
			return "login";
		}
		if(drug.getDrugname()!=null){
			if(drug.getPrice()==0.00){
				drug.setDrugcan(false);
			}else{
				drug.setDrugcan(true);
			}
			drugService.updateDrug(drug);
			ShowInformation.forInformation(model, "修改成功", "返回", request.getContextPath()+"/drugManager/getDrugList", 4);
			return "information";
		}else{
			Drug rowDrug = drugService.getDrugByDrugId(drugId);
			model.addAttribute("drug",rowDrug);
			model.addAttribute("drugsForLevelOne", drugService.getAllFirstLevelDrug());
			return "/medicine/modifyMedicineMessage";
		}
		
		
	}
	
	

	
	
	//下面是开卡工作的,现在都交给药房管理员处理了
	/**
	 * 为页面提供一个页面对象，提供给病人列表
	 * 为页面提供一个空的病人对象
	 * @param model
	 */
	@ModelAttribute()
	public void init(Model model,Page page,Patient patient){
		if(patient==null){
			model.addAttribute("patient", patient);
		}
				
	}
	
	
	/**
	 * 分页查询patient列表
	 * @param model 为应用容器添加值
	 * @param pageNo 获取该列表显示的页面
	 * @return
	 */
	@RequestMapping("/getPatientList")
	public String getPatientList(Model model,String pageNo,@ModelAttribute("user")User user,@ModelAttribute("permission")int permission){
		if(user==null||permission!=3){
			return "login";
		}
		Page page = new Page();
		if(pageNo==""||pageNo==null){
			page.setPageNo(0);
		}else{
			
			page.setPageNo(Integer.valueOf(pageNo).intValue());
		}
		page.setPageSize(10);
		page.setTotalRecords(patientService.getCountOfPatients());
		List<Patient> list = patientService.getPatients(page);
		model.addAttribute("allPageNo", page.getTotalPage());
		model.addAttribute("patients", list);
		return "/card/cardList";
		
		
	}
	
	//添加病人
	@RequestMapping("/addPatient")
	public String addPatientList(HttpServletRequest request,Model model,Patient patient,@ModelAttribute("user")User user,@ModelAttribute("permission")int permission){
		if(user==null||permission!=3){
			return "login";
		}
		if(patient.getCardid()==null||patient.getCardid().trim()==""){
			return "/card/addCard";
		}else{
			patientService.addPatient(patient);
			ShowInformation.forInformation(model, "添加成功", "返回", request.getContextPath()+"/drugManager/addPatient", 4);
			return "/information";
		}
		
		
		
	}
	/**
	 * 删除病人
	 * @param request
	 * @param model
	 * @param patientId
	 * @param user
	 * @param permission
	 * @return
	 */
	@RequestMapping("/deletePatient")
	public String delectPatientList(HttpServletRequest request,Model model,String patientId,@ModelAttribute("user")User user,@ModelAttribute("permission")int permission){
		if(user==null||permission!=3){
			return "/login";
		}
		
		patientService.delectPatient(Integer.valueOf(patientId).intValue());
		ShowInformation.forInformation(model, "删除成功", "返回", request.getContextPath()+"/drugManager/getPatientList", 4);
		return "/information";
	}
	
	
	/**
	 * 更新病人信息
	 * @param request
	 * @param model
	 * @param patient
	 * @param patientId
	 * @param user
	 * @param permission
	 * @return
	 */
	@RequestMapping("/updatePatient")
	public String updatePatientList(HttpServletRequest request,Model model,Patient patient,String patientId,@ModelAttribute("user")User user,@ModelAttribute("permission")int permission){
		if(user==null||permission!=3){
			return "/login";
		}
		if(patient.getPatientid()!=null){
			patientService.updatePatient(patient);
			ShowInformation.forInformation(model, "修改成功", "返回",request.getContextPath()+"/drugManager/getPatientList", 4);
			return "information";
		}else{
			Patient rowPatient =patientService.getPatientByPatientId(patientId);
			model.addAttribute("patient", rowPatient);
			return "/card/modifyCardMessage";
		}
		
		
	
	}
	
	
	/**
	 * 设置绑定器
	 * @param binder
	 * @throws ServletException
	 */
	 @InitBinder
	    protected void initBinder(
	            WebDataBinder binder) throws ServletException {
	       
	         
	        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
	                dateFormat.setLenient(false);
	                binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
	                
	               
	    }
	
	
	
	
	
	
	
	
	
	
}
